`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    09:35:49 11/25/2013 
// Design Name: 
// Module Name:    cavlc_decoder 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module cavlc_decoder(
    Clk, 
    Rst,
    Code,
    Sig_start,
    Sig_done,
    Sig_code_readed,
    NC,
    Level_0,
    Level_1,
    Level_2,
    Level_3,
    Level_4,
    Level_5,
    Level_6,
    Level_7,
    Level_8,
    Level_9,
    Level_10,
    Level_11,
    Level_12,
    Level_13,
    Level_14,
    Level_15,
    TotalLength
    //DEBUG
    ,
    current_state,
    Code_spliced,
    Coeff_token_code,
    Tls_code,
    Total_zero_code,
    Level_code,
    Run_before_code,
    curLength,
    TotalZero_out,
    TotalCoeff_out,
	Tls_out,
    TotalZero_in,
    TotalCoeff_in,
	Tls_in,
    Level_0_Tls_out,
    Level_1_Tls_out,
    Level_2_Tls_out,
    Code_t,
    Bound
    //
    );
    parameter Level_Length = 16;
	input Clk,Rst;
    input [63:0] Code;
    input [4:0] NC;
    input Sig_start;
    
    output Sig_code_readed;
    output Sig_done;
    output [Level_Length-1:0] Level_0;
    output [Level_Length-1:0] Level_1;
    output [Level_Length-1:0] Level_2;
    output [Level_Length-1:0] Level_3;
    output [Level_Length-1:0] Level_4;
    output [Level_Length-1:0] Level_5;
    output [Level_Length-1:0] Level_6;
    output [Level_Length-1:0] Level_7;
    output [Level_Length-1:0] Level_8;
    output [Level_Length-1:0] Level_9;
    output [Level_Length-1:0] Level_10;
    output [Level_Length-1:0] Level_11;
    output [Level_Length-1:0] Level_12;
    output [Level_Length-1:0] Level_13;
    output [Level_Length-1:0] Level_14;
    output [Level_Length-1:0] Level_15;
    output [9:0] TotalLength;
    //DEBUG
    output [15:0] Coeff_token_code;
    output [2:0] Tls_code;
    output [8:0] Total_zero_code;
    output [63:0] Level_code;
    output [63:0] Run_before_code;
    output [9:0] curLength;
    output [3:0] TotalZero_out;
    output [4:0] TotalCoeff_out;
	output [2:0] Tls_out;
    
    output [3:0] TotalZero_in;
    output [4:0] TotalCoeff_in;
	output [2:0] Tls_in;
    
    output [Level_Length-1:0] Level_0_Tls_out;
    output [Level_Length-1:0] Level_1_Tls_out;
    output [Level_Length-1:0] Level_2_Tls_out;
    output [7:0] Bound;
    wire [9:0] curLength;
    wire [7:0] Bound;
    //
    wire Clk,Rst;
    wire [4:0] NC;
    wire [Level_Length-1:0] Level_0_Tls_in;
    wire [Level_Length-1:0] Level_1_Tls_in;
    wire [Level_Length-1:0] Level_2_Tls_in;
    wire [Level_Length-1:0] Level_0_Tls_out;
    wire [Level_Length-1:0] Level_1_Tls_out;
    wire [Level_Length-1:0] Level_2_Tls_out;
    wire Sig_done;
    wire Sig_start;
    wire Sig_level_decode_start;
    wire Sig_level_decode_done;
    wire Sig_runbefore_decode_start;
    wire Sig_runbefore_decode_done;
    wire Sig_level_fill;
    wire Sig_runbefore_fill;
    wire Sig_level_fill_done;
    wire Sig_runbefore_fill_done;
    wire Sig_code_readed;
    wire Sig_code_read_done;
    wire [3:0] Level_cnt;
    wire [3:0] level_cnt_t;
    wire [3:0] CodeLength;
    
    wire [Level_Length-1:0] Level_0;
    wire [Level_Length-1:0] Level_1;
    wire [Level_Length-1:0] Level_2;
    wire [Level_Length-1:0] Level_3;
    wire [Level_Length-1:0] Level_4;
    wire [Level_Length-1:0] Level_5;
    wire [Level_Length-1:0] Level_6;
    wire [Level_Length-1:0] Level_7;
    wire [Level_Length-1:0] Level_8;
    wire [Level_Length-1:0] Level_9;
    wire [Level_Length-1:0] Level_10;
    wire [Level_Length-1:0] Level_11;
    wire [Level_Length-1:0] Level_12;
    wire [Level_Length-1:0] Level_13;
    wire [Level_Length-1:0] Level_14;
    wire [Level_Length-1:0] Level_15;

    wire [Level_Length-1:0] Level_0_ld;
    wire [Level_Length-1:0] Level_1_ld;
    wire [Level_Length-1:0] Level_2_ld;
    wire [Level_Length-1:0] Level_3_ld;
    wire [Level_Length-1:0] Level_4_ld;
    wire [Level_Length-1:0] Level_5_ld;
    wire [Level_Length-1:0] Level_6_ld;
    wire [Level_Length-1:0] Level_7_ld;
    wire [Level_Length-1:0] Level_8_ld;
    wire [Level_Length-1:0] Level_9_ld;
    wire [Level_Length-1:0] Level_10_ld;
    wire [Level_Length-1:0] Level_11_ld;
    wire [Level_Length-1:0] Level_12_ld;
    wire [Level_Length-1:0] Level_13_ld;
    wire [Level_Length-1:0] Level_14_ld;
    wire [Level_Length-1:0] Level_15_ld;
    
    wire [4:0] Coeff_token_code_len;
    wire [3:0] Total_zero_code_len;
    wire [9:0] Level_code_len;
    wire [7:0] Run_before_code_len;
    wire [4:0] TotalCoeff_in;
	wire [2:0] Tls_in;
    wire [4:0] TotalCoeff_out;
	wire [2:0] Tls_out;
    wire [15:0] Coeff_token_code;
    wire [2:0] Tls_code;
    wire [8:0] Total_zero_code;
    wire [63:0] Level_code;
    wire [63:0] Run_before_code;
    wire [3:0] TotalZero_in;
    wire [3:0] TotalZero_out;
    //DEBUG
    output [15:0] current_state;
    wire [15:0] current_state;
    output [63:0] Code_spliced;
    output [63:0] Code_t;
    wire [63:0] Code_t;
    //output [15:0] Coeff_token_code;
    //output [2:0] Tls_code;
    //output [8:0] Total_zero_code;
    //output [63:0] Level_code;
    //output [63:0] Run_before_code;
    //output [9:0] curLength;
    //output [3:0] TotalZero_out;
    //output [4:0] TotalCoeff_out;
	//output [2:0] Tls_out;
    // 
    cavlc_control_module cavlc_control_module (
    .Clk(Clk), //
    .Rst(Rst), //
    .Level_0_in(Level_0_Tls_in), //
    .Level_1_in(Level_1_Tls_in), //
    .Level_2_in(Level_2_Tls_in), //
    .Level_0_out(Level_0_Tls_out), //
    .Level_1_out(Level_1_Tls_out), //
    .Level_2_out(Level_2_Tls_out), //
    .Sig_start(Sig_start), //
    .Sig_done(Sig_done), //
    .Code(Code), //
    .Sig_code_readed(Sig_code_readed), //
    .Sig_code_read_done(Sig_code_read_done), //
    .Sig_level_decode_start(Sig_level_decode_start), //
    .Sig_level_decode_done(Sig_level_decode_done), //
    .Sig_runbefore_decode_start(Sig_runbefore_decode_start), //
    .Sig_runbefore_decode_done(Sig_runbefore_decode_done), //
    .Sig_level_fill(Sig_level_fill), //
    .Sig_runbefore_fill(Sig_runbefore_fill), //
    .Sig_level_fill_done(Sig_level_fill_done), //
    .Sig_runbefore_fill_done(Sig_runbefore_fill_done), //
    .TotalCoeff_in(TotalCoeff_in), //
    .Tls_in(Tls_in), //
    .TotalZero_in(TotalZero_in), //
    .TotalZero_out(TotalZero_out), //
    .TotalCoeff_out(TotalCoeff_out), //
    .Tls_out(Tls_out), //
    .Coeff_token_code_len(Coeff_token_code_len), //
    .Total_zero_code_len(Total_zero_code_len), //
    .Level_code_len(Level_code_len), //
    .Run_before_code_len(Run_before_code_len), //
    .Coeff_token_code(Coeff_token_code), //
    .Tls_code(Tls_code), //
    .Total_zero_code(Total_zero_code), //
    .Level_code(Level_code), //
    .Run_before_code(Run_before_code), //
    .TotalLength(TotalLength) //
    //DEBUG
    ,
    .current_state(current_state),
    .Code_spliced(Code_spliced),
    .curLength(curLength),
    .Code_t(Code_t),
    .Bound(Bound)
    //
    );
    
    coeff_token_decoder coeff_token_decoder (
    .Code(Coeff_token_code),  //
    .NC(NC), //
    .Tls(Tls_in), // 
    .TotalCoeff(TotalCoeff_in), //
    .Bits(Coeff_token_code_len) //
    );
    
    level_decoder level_decoder (
    .Code(Level_code), //
    .TotalCoeff(TotalCoeff_out), //
    .Tls(Tls_out), //
    .Clk(Clk), //
    .Rst(Rst), //
    .Sig_start(Sig_level_decode_start), //
    .Sig_done(Sig_level_decode_done), //
    .Sig_code_fill_done(Sig_level_fill_done), //
    .Sig_code_fill(Sig_level_fill), //
    .Level_code_length(Level_code_len), //
    .Level_cnt(Level_cnt), //
    .Level_0_in(Level_0_Tls_out), //
    .Level_1_in(Level_1_Tls_out), //
    .Level_2_in(Level_2_Tls_out), //
    .Level_0(Level_0_ld), //
    .Level_1(Level_1_ld), //
    .Level_2(Level_2_ld), //
    .Level_3(Level_3_ld), //
    .Level_4(Level_4_ld), //
    .Level_5(Level_5_ld), //
    .Level_6(Level_6_ld), //
    .Level_7(Level_7_ld), //
    .Level_8(Level_8_ld), //
    .Level_9(Level_9_ld), //
    .Level_10(Level_10_ld), //
    .Level_11(Level_11_ld), //
    .Level_12(Level_12_ld), //
    .Level_13(Level_13_ld), //
    .Level_14(Level_14_ld), //
    .Level_15(Level_15_ld) //
    );
    
    run_before_decoder run_before_decoder (
    .Code(Run_before_code), //
    .Clk(Clk), //
    .Rst(Rst), //
    .Sig_start(Sig_runbefore_decode_start), //
    .Sig_done(Sig_runbefore_decode_done), //
    .Sig_code_fill_done(Sig_runbefore_fill_done), //
    .Sig_code_fill(Sig_runbefore_fill), //
    .TotalCoeff(TotalCoeff_out), // 
    .TotalZero(TotalZero_out), //
    .RunBefore_code_length(Run_before_code_len), //
    .Level_0(Level_0_ld), //
    .Level_1(Level_1_ld), //
    .Level_2(Level_2_ld), //
    .Level_3(Level_3_ld), //
    .Level_4(Level_4_ld), //
    .Level_5(Level_5_ld), //
    .Level_6(Level_6_ld), //
    .Level_7(Level_7_ld), //
    .Level_8(Level_8_ld), //
    .Level_9(Level_9_ld), //
    .Level_10(Level_10_ld), //
    .Level_11(Level_11_ld), //
    .Level_12(Level_12_ld), //
    .Level_13(Level_13_ld), //
    .Level_14(Level_14_ld), //
    .Level_15(Level_15_ld), //
    .Level_0_de(Level_0), //
    .Level_1_de(Level_1), //
    .Level_2_de(Level_2), //
    .Level_3_de(Level_3), //
    .Level_4_de(Level_4), //
    .Level_5_de(Level_5), //
    .Level_6_de(Level_6), //
    .Level_7_de(Level_7), //
    .Level_8_de(Level_8), //
    .Level_9_de(Level_9), //
    .Level_10_de(Level_10), //
    .Level_11_de(Level_11), //
    .Level_12_de(Level_12), //
    .Level_13_de(Level_13), //
    .Level_14_de(Level_14), //
    .Level_15_de(Level_15), //
    .level_cnt_t(level_cnt_t), //
    .CodeLength(CodeLength) //
    );
    
    tls_decoder tls_decoder (
    .Code(Tls_code), //
    .Tls(Tls_out), // 
    .Level_0(Level_0_Tls_in), //
    .Level_1(Level_1_Tls_in), //
    .Level_2(Level_2_Tls_in) //
    );
    
    total_zero_decoder total_zero_decoder (
    .Code(Total_zero_code), //
    .TotalCoeff(TotalCoeff_out), //
    .TotalZero(TotalZero_in), //
    .Bits(Total_zero_code_len) //
    );
endmodule
